package com.sihai.ehr.web.action.salary;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.SalaryPerformance;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.service.SalaryPerformanceService;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.BeanUtils;

import java.util.Iterator;
import java.util.List;

/**
 * author: wxn
 * since: 13-1-16 Time: 上午11:11
 */
@Results({
        @Result(name = "main", location = "salary_main.jsp"),
        @Result(name = "add", location = "performance_add.jsp"),
        @Result(name = "list", location = "performance_list.jsp")
})
public class SalaryPerformanceAction extends EHRAction{
    @BeanInject
    private SalaryPerformanceService salaryPerformanceService;
    @BeanInject
    private OrganizationService organizationService;

    private SalaryPerformance performance;
    private LimitedList<SalaryPerformance> performances;

    private List<Organization> organizations;

    private String result;

    @Action("/salary/performance_main")
    @Override
    public String execute() throws Exception {
        listAction = "/salary/performance_list.xhtml";
        listTitle = "绩效奖金管理";

        organizations = organizationService.findChildren(null, true, null, null);
        if (organizations != null && organizations.size() > 0) {
        	if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
            organizationId = organizations.get(0).getId();
        }
        return "main";
    }

    @Action("/salary/performance_list")
    public String list()throws Exception{
        performances = salaryPerformanceService.find(organizationId, createLimit(), createSorter());
        paging(performances);
        setPageUrl("salary/performance_list.xhtml");
        return "list";
    }

    @Action("/salary/performance_edit")
    public String input()throws Exception{
        if(performance != null && performance.getId() != null){
            performance = salaryPerformanceService.get(performance.getId());
        }
        return "add";
    }

    @Action("/salary/performance_save")
    public String save()throws Exception{
        if(performance != null){
            if(performance.getId() == null){//create
                salaryPerformanceService.save(performance);
            }else{//edit
                SalaryPerformance persistent = salaryPerformanceService.get(performance.getId());
                BeanUtils.copyProperties(performance, persistent, new String[]{"id", "organization"});
                salaryPerformanceService.save(persistent);
            }
        }
        return execute();
    }
    @Action("/salary/performance_delete")
    public String delete()throws Exception{
        String deleteIds = getParameter("deleteIds");
        if(StringUtils.isNotBlank(deleteIds)){
            salaryPerformanceService.delete(deleteIds);
        }
        return execute();
    }

    //检查概数区间
    @Action(value = "/salary/performance_check", results = {@Result(name = "success", type = "json", params = {"root", "result"})})
    public String checkScorePart(){
        Float scoreEnd = performance.getScoreEnd();
        Float scoreStart = performance.getScoreStart();
        if(performance.getOrganization() == null || performance.getOrganization().getId() == null){
            Organization o = new Organization();
            o.setId(organizationId);
            performance.setOrganization(o);
        }
        if(performance == null || scoreEnd == null || scoreStart == null){
            result = "input_error";
            return SUCCESS;
        }
        if(scoreStart > 100 || scoreStart < 0 || scoreEnd > 100 || scoreEnd < 0 || scoreEnd <= scoreStart){
            result = "data_error";
            return SUCCESS;
        }
        boolean f = salaryPerformanceService.check(performance);
        result = (f ? "success" : "error");
        return SUCCESS;
    }

    public List<Organization> getOrganizations() {
        return organizations;
    }

    public void setOrganizations(List<Organization> organizations) {
        this.organizations = organizations;
    }

    public LimitedList<SalaryPerformance> getPerformances() {
        return performances;
    }

    public void setPerformances(LimitedList<SalaryPerformance> performances) {
        this.performances = performances;
    }

    public SalaryPerformance getPerformance() {
        return performance;
    }

    public void setPerformance(SalaryPerformance performance) {
        this.performance = performance;
    }

    public String getResult() {
        return result;
    }
}
